const path = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const { CleanWebpackPlugin } = require("clean-webpack-plugin")

module.exports = {
  // 入口
  entry: "./src/index.ts",
  // 出口:指定打包文件所在目录
  output: {
    // 指定打包文件的目录
    path: path.resolve(__dirname, 'dist'),
    // 打包后文件名称
    filename: "bundle.js",
  },
  resolve: {
    extensions: [".ts", ".js"]
  },
  // mode(模式) 只能是 development | production | none
  mode: 'development',
  // 指定 webpack 打包时要使用的模块
  module: {
    // rules指定要加载的规则
    rules: [
      {
        // 指定的是规则生效的文件
        test: /\.ts/,
        // 要使用的loader
        use: [
          {
            loader:"babel-loader",
            options:{
              presets:[
                [
                  "@babel/preset-env",
                  {
                    "targets":{
                      "chrome":"58",
                      "ie":"11"
                    },
                    "corejs":"3",
                    "useBuiltIns":"usage"
                  }
                ]
              ]
            }
          },
          {
            loader: "ts-loader"
          }
        ],
        // 要排除的文件
        exclude: /node_modules/
      },
      {
        test: /\.less$/,
        use:[
          "style-loader",
          "css-loader",
          {
            loader: "postcss-loader",
            options: {
              postcssOptions: {
                plugins:[
                  [
                    "postcss-preset-env",
                    {browsers: 'last 2 versions'}
                  ]
                ]
              }
            }
          },
          "less-loader"
        ]
      }
    ]
  },
  plugins: [
    new HtmlWebpackPlugin({
      template:'./src/index.html'
    }),
    new CleanWebpackPlugin()
  ]
}